/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.db.dao.UserDao;
import com.gmail.blackdog1987.carcare.entity.TProduct;
import com.gmail.blackdog1987.carcare.network.request.builder.OrderDriveCompanyRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.IResponse;
import com.gmail.blackdog1987.carcare.network.response.OrderDriveCompanyResponse;
import com.gmail.blackdog1987.carcare.network.response.OrderMeirongResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.gmail.blackdog1987.carcare.network.response.handler.OrderDriveCompanyResponseHandler;

/**
 * @author wdy
 * 
 */
public class OrderLicenseActivity extends BaseActivity implements OnClickListener, OnResponseListener {
	private Context mContext;
	private EditText mName;
	private EditText mPhone;
	private EditText mStartpoint;
	private EditText mEndpoint;
	private TextView mTime;
	private long time;
	private View mPay;
	private OrderDriveCompanyRequestBuilder mReq = new OrderDriveCompanyRequestBuilder();
	private OrderDriveCompanyResponseHandler mHandler = new OrderDriveCompanyResponseHandler();

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_company);
		mContext = this;

		initViews();
		initData(savedInstanceState);
		reg();
	}

	public void initViews() {
		mName = (EditText) findViewById(R.id.company_name);
		mPhone = (EditText) findViewById(R.id.company_phone);
		mStartpoint = (EditText) findViewById(R.id.company_startpoint);
		mEndpoint = (EditText) findViewById(R.id.company_endpoint);
		mTime = (TextView) findViewById(R.id.company_time);
		mPay = findViewById(R.id.company_pay);
	}

	public void reg() {
		mPay.setOnClickListener(this);
		mTime.setOnClickListener(this);
		findViewById(R.id.company_location).setOnClickListener(this);
		findViewById(R.id.company_call).setOnClickListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		mHandler.setOnResponseListener(this);
	}

	public void initData(Bundle savedInstanceState) {
	}

	private boolean validInput() {
		return true;
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
//		case R.id.company_location:
//			Intent intent = new Intent(OrderCleanActivity.this, MapGuidActivity.class);
//			intent.putExtra("lat", shop.lat);
//			intent.putExtra("lng", shop.lon);
//			startActivity(intent);
//			break;
//		case R.id.company_call:
//			if(shop.phone==null||shop.phone.length()==0){
//				Toast.makeText(mContext, "尚未录入商家电话", Toast.LENGTH_LONG).show();
//				return;
//			}
//			Intent callintent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+shop.phone));  
//            startActivity(callintent);
//			break;
		case R.id.back:
			finish();
			break;
		case R.id.company_time:
			break;
		case R.id.company_pay:
			if (!App.validUser(OrderLicenseActivity.this))
				return;
			if (validInput()) {
				mReq = new OrderDriveCompanyRequestBuilder();
				mReq.setEndpoint(mEndpoint.getText().toString()).setStartpoint(mStartpoint.getText().toString()).setPhone(mPhone.getText().toString())
						.setName(mName.getText().toString()).setTime(String.valueOf(time)).postJsonRequest(mContext, mHandler);
			}
			break;
		default:
			break;
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case PayActivity.REQUESTCODE_PAY:
				Toast.makeText(mContext, "恭喜您支付成功", Toast.LENGTH_LONG).show();
				finish();
				break;
			}
		}
	}

	@Override
	public void onResponse(IResponseHandler classz) {
		IResponse res = classz.getResponse();
		if(res instanceof OrderDriveCompanyResponse){
			TProduct product = new TProduct();
			product.body = "驾校陪练";
			product.price = 120.0;
			product.subject = "车拉丁";
			product.tableName = "t_order_drivecompany";
			product.tableId = ((OrderDriveCompanyResponse)classz.getResponse()).order.getId();
			product.userid = new UserDao().getUser().id;
			Intent i = new Intent(OrderLicenseActivity.this, PayActivity.class);
			i.putExtra("data", product);
			startActivityForResult(i, PayActivity.REQUESTCODE_PAY);
		}
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "下单失败！", Toast.LENGTH_LONG).show();
	}
}
